package com.pur.zsms.ordercustommanage.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;

import org.apache.commons.beanutils.BeanUtils;
import org.exolab.castor.types.Date;
import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.foodmanage.food.po.FoodMaterPo;
import com.pur.zsms.foodmanage.food.po.FoodPo;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.matermanage.mater.dao.IMaterDao;
import com.pur.zsms.matermanage.mater.po.MaterLevelBlPo;
import com.pur.zsms.matermanage.mater.po.MaterLevelPo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.ordercustommanage.dao.ISelectFoodDao;
import com.pur.zsms.ordercustommanage.po.OrderCustFoodPo;
import com.pur.zsms.ordercustommanage.po.OrderCustPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterNeedPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterRealPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.ordercustommanage.service.ISelectFoodService;
import com.pur.zsms.ordercustommanage.vo.OrderCustFoodVo;
import com.pur.zsms.ordercustommanage.vo.OrderCustVo;
import com.pur.zsms.ordercustommanage.vo.OrderStateVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierMaterRealVo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierVo;
import com.pur.zsms.ordercustommanage.vo.SelectFoodVo;
import com.pur.zsms.ordercustommanage.vo.SelectMaterVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.shopmanage.dao.IShopDao;
import com.pur.zsms.supplier.dao.SupplierOrderDao;
import com.pur.zsms.supplier.po.ShopSupplierTypePo;
import com.pur.zsms.supplier.po.SupplierPo;

@Service("selectFoodService")
public class SelectFoodServiceImpl implements ISelectFoodService {
	@Resource(name = "supplierOrderDao")
	private SupplierOrderDao supplierOrderDao;

	@Resource(name = "selectFoodDao")
	private ISelectFoodDao selectFoodDaoImpl;

	@Resource(name = "materDao")
	private IMaterDao materDaoImpl;

	@Resource(name = "shopDao")
	private IShopDao shopDaoImpl;

	public IMaterDao getMaterDaoImpl() {
		return materDaoImpl;
	}

	public void setMaterDaoImpl(IMaterDao materDaoImpl) {
		this.materDaoImpl = materDaoImpl;
	}

	public ISelectFoodDao getSelectFoodDaoImpl() {
		return selectFoodDaoImpl;
	}

	public void setSelectFoodDaoImpl(ISelectFoodDao selectFoodDaoImpl) {
		this.selectFoodDaoImpl = selectFoodDaoImpl;
	}

	String msg = "";

	public SupplierOrderDao getSupplierOrderDao() {
		return supplierOrderDao;
	}

	public void setSupplierOrderDao(SupplierOrderDao supplierOrderDao) {
		this.supplierOrderDao = supplierOrderDao;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Override
	public int addOrder(List<SelectFoodVo> selectFoodList, UserPo po) throws GenericException {
		OrderCustPo custPo = new OrderCustPo();
		custPo.setOrdCustId((int) po.getUserId());
		custPo.setOrdState(1);
		custPo.setUpdateperson((int) po.getUserId());
		custPo.setOrdDate(selectFoodList.get(0).getOrderDate());
		custPo.setOrdDay(selectFoodList.get(0).getOrderDay());
		// ---------------计算菜的总价钱--------------∨
		BigDecimal allValue = new BigDecimal(0);
		for (int i = 0; i < selectFoodList.size(); i++) {
			BigDecimal value = selectFoodDaoImpl.getFootValueByFoodId(selectFoodList.get(i).getFoodId(), (int) po.getUserId());
			BigDecimal fraction = new BigDecimal(selectFoodList.get(i).getFoodNum());
			allValue = allValue.add(value.multiply(fraction));
		}
		// ---------------计算菜的总价钱--------------∧
		custPo.setOrdValue(allValue);
		selectFoodDaoImpl.addSelectFood(custPo);
		return custPo.getOrdId();
	}

	@Override
	public int findInsertId() throws GenericException {
		return selectFoodDaoImpl.findInsertId();
	}

	@Override
	public PaginationSupport listMyOrder(PageController pageController, OrderCustVo orderCustVo) throws GenericException {
		return selectFoodDaoImpl.listMyOrder(pageController, orderCustVo);
	}

	@Override
	public PaginationSupport findEveryOrderState(PageController pageController, OrderStateVo orderStateVo) throws GenericException {
		return selectFoodDaoImpl.findEveryOrderState(pageController, orderStateVo);
	}

	@Override
	public PaginationSupport listAllOrderForpurchase(PageController pageController, OrderCustVo orderCustVo) throws GenericException {
		return selectFoodDaoImpl.listAllOrderForpurchase(pageController, orderCustVo);
	}

	@Override
	public PaginationSupport reviewOrderList(PageController pageController, OrderCustVo orderCustVo) throws GenericException {
		return selectFoodDaoImpl.reviewOrderList(pageController, orderCustVo);
	}

	@Override
	public void passOrder(OrderCustVo c) throws GenericException {
		OrderCustPo po = new OrderCustPo();
		try {
			BeanUtils.copyProperties(po, c);
		} catch (Exception e) {
			throw new RuntimeException("更新失败!");
		}
		po.setOrdState(2);
		selectFoodDaoImpl.passOrder(po);
	}

	public void passOrderValueForPurchase(OrderSupplierVo c) throws GenericException {
		// 去的订单
		List<OrderSupplierPo> list = selectFoodDaoImpl.getOrderSupplierPoByOrdIdAndSupId(String.valueOf(c.getOrdId()),
				String.valueOf(c.getOrdSupId()));
		OrderSupplierPo tOrderSupplierPo = list.get(0);
		tOrderSupplierPo.setOrdState(5);
		selectFoodDaoImpl.updateOrderSupplier(tOrderSupplierPo);

	}

	@Override
	public void nopassOrder(OrderCustVo c) throws GenericException {
		OrderCustPo po = new OrderCustPo();
		try {
			BeanUtils.copyProperties(po, c);
		} catch (Exception e) {
			throw new RuntimeException("更新失败!");
		}
		po.setOrdState(3);
		selectFoodDaoImpl.passOrder(po);
	}

	@Override
	public void passMaterOrder(OrderCustVo c) throws GenericException {
		OrderCustPo po = new OrderCustPo();
		try {
			BeanUtils.copyProperties(po, c);
		} catch (Exception e) {
			throw new RuntimeException("更新失败!");
		}
		po.setOrdState(4);
		selectFoodDaoImpl.passOrder(po);
	}

	@Override
	public OrderCustVo getOrderCust(String c) throws GenericException {
		OrderCustPo po = null;
		po = selectFoodDaoImpl.getOrderCust(c);
		// zhouziyi
		// po.setOrdCommitdate(new BigDecimal(1));
		// po.setUpdateperson(1);
		po.setUpdateversion("1");
		// zhouziyi

		OrderCustVo vo = new OrderCustVo();
		try {
			BeanUtils.copyProperties(vo, po);
		} catch (Exception e) {
			throw new RuntimeException("查询失败!");
		}
		return vo;
	}

	public SelectMaterVo getOrderCustForUpdateSelect(int ordId) throws GenericException {
		OrderCustPo po = null;
		po = selectFoodDaoImpl.getOrderCust(String.valueOf(ordId));
		SelectMaterVo tSelectMaterVo = new SelectMaterVo();

		BeanCopier copier = BeanCopier.create(OrderCustPo.class, SelectMaterVo.class, false);
		copier.copy(po, tSelectMaterVo, null);

		tSelectMaterVo.setOrdCustName(shopDaoImpl.getShopPoByShopId(po.getOrdCustId()).get(0).getShopName());

		return tSelectMaterVo;
	}

	@Override
	public boolean judgeFoodOrMaterHtml(String ordId) throws GenericException {
		boolean flag = false;
		List<OrderCustFoodPo> poList = null;
		poList = selectFoodDaoImpl.getOrderCustFood(ordId);
		for (OrderCustFoodPo orderCustFoodPo : poList) {
			if (orderCustFoodPo.getOrdMaterLevel() == 0) {
				flag = true;
			} else {
				flag = false;
			}
		}

		return flag;
	}

	@Override
	public List<OrderCustFoodVo> getOrderCustFood(String c) throws GenericException {
		List<OrderCustFoodPo> poList = null;
		poList = selectFoodDaoImpl.getOrderCustFood(c);
		// zhouziyi
		// po.setOrdCommitdate(new BigDecimal(1));
		// po.setUpdateperson(1);
		// poList.setUpdateversion("1");
		// zhouziyi

		List<OrderCustFoodVo> voList = new ArrayList<OrderCustFoodVo>();
		try {
			for (int i = 0; i < poList.size(); i++) {
				OrderCustFoodVo vo = new OrderCustFoodVo();
				BeanUtils.copyProperties(vo, poList.get(i));
				voList.add(vo);
			}

		} catch (Exception e) {
			throw new RuntimeException("查询失败!");
		}
		return voList;
	}

	@Override
	public List<OrderSupplierMaterPo> getOrderSupplierMaterPoByFoodIdAndMateridAndOid(String foodId, String materId, String ordId)
			throws GenericException {
		return selectFoodDaoImpl.getOrderSupplierMaterPoByFoodIdAndMateridAndOid(foodId, materId, ordId);
	}

	@Override
	public List<OrderSupplierMaterPo> getOrderSupplierMaterPoByMateridOrdId(int materId, int ordId) throws GenericException {
		return selectFoodDaoImpl.getOrderSupplierMaterPoByMateridOrdId(materId, ordId);
	}

	@Override
	public List<OrderSupplierMaterPo> getOrderSupplierMaterPoByMateridOrdId(int materId, int ordId, int supid) throws GenericException {
		return selectFoodDaoImpl.getOrderSupplierMaterPoByMateridOrdId(materId, ordId, supid);
	}

	/**
	 * 校验发货用
	 * */
	public boolean getOrderSupplierPoByOrdIdForCheck(String ordId) throws GenericException {
		List<OrderSupplierPo> list = selectFoodDaoImpl.getOrderSupplierPoByOrdIdForCheck(ordId);
		if (list != null && list.size() > 0) {
			return false;
		}

		return true;
	}

	/**
	 * 校验发货用
	 * */
	public boolean getOrderSupplierPoByOrdIdAndSupIdForCheck(String ordId, String supId) throws GenericException {
		List<OrderSupplierPo> list = selectFoodDaoImpl.getOrderSupplierPoByOrdIdAndSupIdForCheck(ordId, supId);
		if (list != null && list.size() > 0) {
			return false;
		}

		return true;

	}

	public List<OrderSupplierPo> getOrderSupplierPoByOrdId(String ordId) throws GenericException {
		return selectFoodDaoImpl.getOrderSupplierPoByOrdId(ordId);

	}

	@Override
	public List<OrderSupplierPo> getOrderSupplierPoByOrdIdAndSupId(String OrdId, String SupId) throws GenericException {
		return selectFoodDaoImpl.getOrderSupplierPoByOrdIdAndSupId(OrdId, SupId);
	}

	@Override
	public List<OrderSupplierPo> getOrderSupplierPoByOrdDateAndSupId(String ordDate, int supId) throws GenericException {
		return selectFoodDaoImpl.getOrderSupplierPoByOrdDateAndSupId(ordDate, supId);
	}

	public List<OrderSupplierPo> getOrderSupplierPoByOrdDateAndSupIdAndShopId(String ordDate, int supId, long shopId) throws GenericException {
		return selectFoodDaoImpl.getOrderSupplierPoByOrdDateAndSupIdAndShopId(ordDate, supId, shopId);
	}

	@Override
	public List<FoodMaterPo> getFootMaterByFoodId(int foodId) throws GenericException {
		return selectFoodDaoImpl.getFootMaterByFoodId(foodId);
	}

	@Override
	public MaterPo getMaterValueByMaterId(int materId) throws GenericException {
		return selectFoodDaoImpl.getMaterValueByMaterId(materId);
	}

	@Override
	public MaterLevelPo getMaterLevelByMaterIdAndLevel(int materId, int level) throws GenericException {
		return selectFoodDaoImpl.getMaterLevelByMaterIdAndLevel(materId, level);
	}

	@Override
	public MaterLevelPo getMaterLevelByMaterId(int materId) throws GenericException {
		return selectFoodDaoImpl.getMaterLevelByMaterId(materId);
	}

	@Override
	public void addOrderSupplierMater(OrderSupplierMaterPo orderSupplierMaterPo) throws GenericException {
		selectFoodDaoImpl.addOrderSupplierMater(orderSupplierMaterPo);
	}

	@Override
	public boolean addOrderSupplierMaterReal(List<OrderSupplierMaterRealVo> orderSupplierMaterRealVoList) throws GenericException {
		boolean flag = false;
		// 插入需要进货表

		// 插入实际进货表
		for (OrderSupplierMaterRealVo orderSupplierMaterRealVo : orderSupplierMaterRealVoList) {
			flag = true;
			OrderSupplierMaterRealPo orderSupplierMaterRealPo = new OrderSupplierMaterRealPo();

			BeanCopier copier = BeanCopier.create(OrderSupplierMaterRealVo.class, OrderSupplierMaterRealPo.class, false);
			copier.copy(orderSupplierMaterRealVo, orderSupplierMaterRealPo, null);

			if (orderSupplierMaterRealVo.getOrdMaterWeightReal().compareTo(BigDecimal.ZERO) == 0) {
				continue;
			}
			orderSupplierMaterRealPo.setOrdMaterWeight(orderSupplierMaterRealVo.getOrdMaterWeightReal());
			// 校验状态
			/*
			 * OrderSupplierPo tOrderSupplierPo =
			 * selectFoodDaoImpl.getOrderSupplierPoByOrdId(
			 * orderSupplierMaterRealPo.getOrdId(),
			 * orderSupplierMaterRealPo.getOrdSupId()); if
			 * (tOrderSupplierPo.getOrdState() != 2 &&
			 * !orderSupplierMaterRealVo.isCheckflag()) { msg = "msg005"; return
			 * false; }
			 */
			// 根据实际重量 算出实际钱

			/*
			 * if
			 * (orderSupplierMaterRealVo.getOrdMaterWeightReal().toString().equals
			 * ("0")) { orderSupplierMaterRealPo.setOrdMaterValue(new
			 * BigDecimal(0)); flag = true; } else {
			 * 
			 * List<Object[]> list =
			 * selectFoodDaoImpl.getRealValue(orderSupplierMaterRealPo
			 * .getOrdId(), orderSupplierMaterRealPo.getOrdMaterLevel(),
			 * orderSupplierMaterRealVo .getOrdMaterWeightReal().toString(),
			 * orderSupplierMaterRealPo.getOrdSupId(),
			 * orderSupplierMaterRealPo.getOrdMaterId()); if (list != null &&
			 * list.size() > 0) { String valuefirst =
			 * String.valueOf(list.get(0)); BigDecimal value = new
			 * BigDecimal(valuefirst);
			 * orderSupplierMaterRealPo.setOrdMaterValue(value); flag = true;
			 * 
			 * } else { msg = "msg006"; return false; } }
			 */
			selectFoodDaoImpl.addOrderSupplierMaterReal(orderSupplierMaterRealPo);
		}
		return flag;

	}
	
	@Override
	public boolean addOrderSupplierMaterReal(List<OrderSupplierMaterRealVo> orderSupplierMaterRealVoList,int ordId) throws GenericException {
		boolean flag = false;
		// 插入需要进货表

		// 插入实际进货表
		for (OrderSupplierMaterRealVo orderSupplierMaterRealVo : orderSupplierMaterRealVoList) {
			flag = true;
			OrderSupplierMaterRealPo orderSupplierMaterRealPo = new OrderSupplierMaterRealPo();

			BeanCopier copier = BeanCopier.create(OrderSupplierMaterRealVo.class, OrderSupplierMaterRealPo.class, false);
			copier.copy(orderSupplierMaterRealVo, orderSupplierMaterRealPo, null);

			if (orderSupplierMaterRealVo.getOrdMaterWeightReal().compareTo(BigDecimal.ZERO) == 0) {
				continue;
			}
			orderSupplierMaterRealPo.setOrdMaterWeight(orderSupplierMaterRealVo.getOrdMaterWeightReal());
			orderSupplierMaterRealPo.setOrdId(ordId);
			// 校验状态
			/*
			 * OrderSupplierPo tOrderSupplierPo =
			 * selectFoodDaoImpl.getOrderSupplierPoByOrdId(
			 * orderSupplierMaterRealPo.getOrdId(),
			 * orderSupplierMaterRealPo.getOrdSupId()); if
			 * (tOrderSupplierPo.getOrdState() != 2 &&
			 * !orderSupplierMaterRealVo.isCheckflag()) { msg = "msg005"; return
			 * false; }
			 */
			// 根据实际重量 算出实际钱

			/*
			 * if
			 * (orderSupplierMaterRealVo.getOrdMaterWeightReal().toString().equals
			 * ("0")) { orderSupplierMaterRealPo.setOrdMaterValue(new
			 * BigDecimal(0)); flag = true; } else {
			 * 
			 * List<Object[]> list =
			 * selectFoodDaoImpl.getRealValue(orderSupplierMaterRealPo
			 * .getOrdId(), orderSupplierMaterRealPo.getOrdMaterLevel(),
			 * orderSupplierMaterRealVo .getOrdMaterWeightReal().toString(),
			 * orderSupplierMaterRealPo.getOrdSupId(),
			 * orderSupplierMaterRealPo.getOrdMaterId()); if (list != null &&
			 * list.size() > 0) { String valuefirst =
			 * String.valueOf(list.get(0)); BigDecimal value = new
			 * BigDecimal(valuefirst);
			 * orderSupplierMaterRealPo.setOrdMaterValue(value); flag = true;
			 * 
			 * } else { msg = "msg006"; return false; } }
			 */
			selectFoodDaoImpl.addOrderSupplierMaterReal(orderSupplierMaterRealPo);
		}
		return flag;

	}

	public boolean addOrderSupplierMaterRealForSelf(List<OrderSupplierMaterRealVo> orderSupplierMaterRealVoList) throws GenericException {
		boolean flag = false;
		// 插入需要进货表

		// 插入实际进货表
		for (OrderSupplierMaterRealVo orderSupplierMaterRealVo : orderSupplierMaterRealVoList) {
			flag = true;
			OrderSupplierMaterRealPo orderSupplierMaterRealPo = new OrderSupplierMaterRealPo();

			BeanCopier copier = BeanCopier.create(OrderSupplierMaterRealVo.class, OrderSupplierMaterRealPo.class, false);
			copier.copy(orderSupplierMaterRealVo, orderSupplierMaterRealPo, null);

			if (orderSupplierMaterRealVo.getOrdMaterWeightReal().compareTo(BigDecimal.ZERO) == 0) {
				continue;
			}
			orderSupplierMaterRealPo.setOrdMaterWeight(orderSupplierMaterRealVo.getOrdMaterWeightReal());
			orderSupplierMaterRealPo.setOrdMaterValueUnit(orderSupplierMaterRealPo.getOrdMaterValue().divide(
					orderSupplierMaterRealPo.getOrdMaterWeight(), 2, BigDecimal.ROUND_HALF_UP));
			selectFoodDaoImpl.addOrderSupplierMaterReal(orderSupplierMaterRealPo);
		}
		return flag;
	}

	public boolean addOrderSupplierMaterRealForUpdate(List<OrderSupplierMaterRealVo> orderSupplierMaterRealVoList) throws GenericException {
		boolean flag = false;
		for (OrderSupplierMaterRealVo orderSupplierMaterRealVo : orderSupplierMaterRealVoList) {
			flag = true;

			OrderSupplierMaterRealPo orderSupplierMaterRealPo = new OrderSupplierMaterRealPo();

			List<OrderSupplierMaterRealPo> listorderSupplierMaterRealPo = supplierOrderDao.getOrderSupMaterRealListByOrdIdMaterId(
					orderSupplierMaterRealVo.getOrdId(), orderSupplierMaterRealVo.getOrdMaterId(), orderSupplierMaterRealVo.getOrdSupId(), 0);
			if (listorderSupplierMaterRealPo != null && listorderSupplierMaterRealPo.size() > 0) {
				orderSupplierMaterRealPo = listorderSupplierMaterRealPo.get(0);

				orderSupplierMaterRealPo.setOrdMaterWeight(orderSupplierMaterRealVo.getOrdMaterWeightReal().add(
						orderSupplierMaterRealPo.getOrdMaterWeight()));
				selectFoodDaoImpl.updateOrderSupplierMaterReal(orderSupplierMaterRealPo);

			} else {
				BeanCopier copier = BeanCopier.create(OrderSupplierMaterRealVo.class, OrderSupplierMaterRealPo.class, false);
				copier.copy(orderSupplierMaterRealVo, orderSupplierMaterRealPo, null);

				// 在前面校验是否为零 改变材料的时候默认为零
				/*
				 * if
				 * (orderSupplierMaterRealVo.getOrdMaterWeightReal().compareTo
				 * (BigDecimal.ZERO) == 0) { continue; }
				 */
				orderSupplierMaterRealPo.setOrdMaterWeight(orderSupplierMaterRealVo.getOrdMaterWeightReal());
				selectFoodDaoImpl.addOrderSupplierMaterReal(orderSupplierMaterRealPo);
			}
		}
		return flag;

	}

	public boolean addOrderSupplierMaterRealForSelfUpdate(List<OrderSupplierMaterRealVo> orderSupplierMaterRealVoList) throws GenericException {
		boolean flag = false;
		for (OrderSupplierMaterRealVo orderSupplierMaterRealVo : orderSupplierMaterRealVoList) {
			flag = true;

			OrderSupplierMaterRealPo orderSupplierMaterRealPo = new OrderSupplierMaterRealPo();

			List<OrderSupplierMaterRealPo> listorderSupplierMaterRealPo = supplierOrderDao.getOrderSupMaterRealListByOrdIdMaterId(
					orderSupplierMaterRealVo.getOrdId(), orderSupplierMaterRealVo.getOrdMaterId());
			if (listorderSupplierMaterRealPo != null && listorderSupplierMaterRealPo.size() > 0) {
				orderSupplierMaterRealPo = listorderSupplierMaterRealPo.get(0);
				orderSupplierMaterRealPo.setOrdMaterValueUnit(orderSupplierMaterRealVo.getOrdMaterValueUnit());
				orderSupplierMaterRealPo.setOrdMaterWeight(orderSupplierMaterRealVo.getOrdMaterWeightReal().add(
						orderSupplierMaterRealPo.getOrdMaterWeight()));
				orderSupplierMaterRealPo.setOrdMaterValue(orderSupplierMaterRealPo.getOrdMaterWeight().multiply(
						orderSupplierMaterRealVo.getOrdMaterValueUnit()));
			} else {
				BeanCopier copier = BeanCopier.create(OrderSupplierMaterRealVo.class, OrderSupplierMaterRealPo.class, false);
				copier.copy(orderSupplierMaterRealVo, orderSupplierMaterRealPo, null);

				if (orderSupplierMaterRealVo.getOrdMaterWeightReal().compareTo(BigDecimal.ZERO) == 0) {
					continue;
				}
				orderSupplierMaterRealPo.setOrdMaterValue(orderSupplierMaterRealPo.getOrdMaterWeight().multiply(
						orderSupplierMaterRealVo.getOrdMaterValueUnit()));
				orderSupplierMaterRealPo.setOrdMaterWeight(orderSupplierMaterRealVo.getOrdMaterWeightReal());
			}

			selectFoodDaoImpl.updateOrderSupplierMaterReal(orderSupplierMaterRealPo);
		}
		return flag;

	}

	@Override
	public boolean updateOrderSupplierMaterReal(List<OrderSupplierMaterRealVo> orderSupplierMaterRealVoList, String ordDate) throws GenericException {
		boolean flag = false;
		// 插入需要进货表

		// 插入实际进货表
		for (OrderSupplierMaterRealVo orderSupplierMaterRealVo : orderSupplierMaterRealVoList) {
			flag = true;

			OrderSupplierMaterRealPo orderSupplierMaterRealPo = new OrderSupplierMaterRealPo();

			List<OrderSupplierMaterRealPo> listorderSupplierMaterRealPo = supplierOrderDao.getOrderSupMaterRealListByOrdIdMaterId(
					orderSupplierMaterRealVo.getOrdId(), orderSupplierMaterRealVo.getOrdMaterId(), orderSupplierMaterRealVo.getOrdSupId(),
					orderSupplierMaterRealVo.getOrdCustId());
			if (listorderSupplierMaterRealPo != null && listorderSupplierMaterRealPo.size() > 0) {
				orderSupplierMaterRealPo = listorderSupplierMaterRealPo.get(0);

				orderSupplierMaterRealPo.setOrdMaterWeight(orderSupplierMaterRealVo.getOrdMaterWeightReal());
			} else {
				BeanCopier copier = BeanCopier.create(OrderSupplierMaterRealVo.class, OrderSupplierMaterRealPo.class, false);
				copier.copy(orderSupplierMaterRealVo, orderSupplierMaterRealPo, null);

				if (orderSupplierMaterRealVo.getOrdMaterWeightReal().compareTo(BigDecimal.ZERO) == 0) {
					continue;
				}
				orderSupplierMaterRealPo.setOrdMaterWeight(orderSupplierMaterRealVo.getOrdMaterWeightReal());
			}

			// 算出实际送货价格
			BigDecimal materValueReal = computeMaterValueReal(orderSupplierMaterRealVo.getOrdMaterId(), orderSupplierMaterRealVo.getOrdSupId(),
					ordDate);
			orderSupplierMaterRealPo.setOrdMaterValue(materValueReal.multiply(orderSupplierMaterRealPo.getOrdMaterWeight()));

			selectFoodDaoImpl.addOrderSupplierMaterReal(orderSupplierMaterRealPo);
		}
		return flag;

	}

	/**
	 * 计算实际送货价钱
	 * */
	public BigDecimal computeMaterValueReal(int materId, int supId, String materDay) throws GenericException {

		BigDecimal materValueReal = new BigDecimal(0);
		List<MaterLevelBlPo> materLevelBlPoList = materDaoImpl.getMaterLevelBlPoByMaterIdAndSupIdAndMaterDay(materId, supId, materDay);
		if (materLevelBlPoList != null && materLevelBlPoList.size() > 0) {
			materValueReal = materLevelBlPoList.get(0).getMaterValueUndressed().multiply(new BigDecimal(1000));
		}
		return materValueReal;
	}

	@Override
	public boolean addOrderSupplierMaterNeed(List<OrderSupplierMaterRealVo> orderSupplierMaterRealVoList) throws GenericException {
		boolean flag = false;
		// 插入需要进货表

		// 插入实际进货表
		for (OrderSupplierMaterRealVo orderSupplierMaterRealVo : orderSupplierMaterRealVoList) {
			flag = true;
			OrderSupplierMaterNeedPo orderSupplierMaterNeedPo = new OrderSupplierMaterNeedPo();

			BeanCopier copier = BeanCopier.create(OrderSupplierMaterRealVo.class, OrderSupplierMaterNeedPo.class, false);
			copier.copy(orderSupplierMaterRealVo, orderSupplierMaterNeedPo, null);

			// 校验状态
			/*
			 * OrderSupplierPo tOrderSupplierPo =
			 * selectFoodDaoImpl.getOrderSupplierPoByOrdId(
			 * orderSupplierMaterNeedPo.getOrdId(),
			 * orderSupplierMaterNeedPo.getOrdSupId()); if
			 * (tOrderSupplierPo.getOrdState() != 2) { msg = "msg005"; return
			 * false; }
			 */
			// 根据实际重量 算出实际钱

			/*
			 * List<Object[]> list =
			 * selectFoodDaoImpl.getRealValue(orderSupplierMaterNeedPo
			 * .getOrdId(), orderSupplierMaterNeedPo.getOrdMaterLevel(),
			 * orderSupplierMaterRealVo.getOrdMaterWeight() .toString(),
			 * orderSupplierMaterNeedPo.getOrdSupId(), orderSupplierMaterNeedPo
			 * .getOrdMaterId()); if (list != null && list.size() > 0) { String
			 * valuefirst = String.valueOf(list.get(0)); BigDecimal value = new
			 * BigDecimal(valuefirst);
			 * orderSupplierMaterNeedPo.setOrdMaterValue(value); flag = true;
			 * 
			 * } else { msg = "msg006"; return false; }
			 */
			selectFoodDaoImpl.addOrderSupplierMaterNeed(orderSupplierMaterNeedPo);
		}
		return flag;

	}

	@Override
	public void updateOrderSupplierMater(OrderSupplierMaterPo orderSupplierMaterPo) throws GenericException {
		selectFoodDaoImpl.updateOrderSupplierMater(orderSupplierMaterPo);
	}

	@Override
	public ShopSupplierTypePo getSuppCusPoByMaterType(int c, int shopId) throws GenericException {
		return selectFoodDaoImpl.getSuppCusPoByMaterType(c, shopId);
	}

	@Override
	public List<Object[]> getOrderSupplierMaterByOrdId(String ordId) throws GenericException {
		return selectFoodDaoImpl.getOrderSupplierMaterByOrdId(ordId);
	}

	public List<Object[]> getOrderSupplierMaterByOrdIdAndSupIdAndShopId(String ordId, long supId, long shopId) throws GenericException {
		return selectFoodDaoImpl.getOrderSupplierMaterByOrdIdAndSupIdAndShopId(ordId, supId, shopId);

	}

	@Override
	public void addOrderSupplier(OrderSupplierPo orderSupplierPo) throws GenericException {
		selectFoodDaoImpl.addOrderSupplier(orderSupplierPo);
	}

	@Override
	public void updateOrderSupplier(OrderSupplierPo orderSupplierPo) throws GenericException {
		selectFoodDaoImpl.updateOrderSupplier(orderSupplierPo);
	}

	@Override
	public OrderCustPo addMaterOrder(List<SelectMaterVo> selectMaterList, long shopId) throws GenericException {
		OrderCustPo orderCustPo = new OrderCustPo();
		orderCustPo.setOrdCustId((int) shopId);
		// 跳过审核状态
		orderCustPo.setOrdState(4);
		orderCustPo.setUpdateperson((int) shopId);
		orderCustPo.setOrdDate(selectMaterList.get(0).getOrderDate());
		orderCustPo.setOrdDay(selectMaterList.get(0).getOrderDay());
		orderCustPo.setSubmitOrdTimeLast(PubFun.getCurrentDate() + " " + PubFun.getCurrentTime());
		// ---------------计算菜的总价钱--------------∨
		BigDecimal allValue = new BigDecimal(0);
		for (int i = 0; i < selectMaterList.size(); i++) {
			BigDecimal value = selectFoodDaoImpl.getMaterLevelByMaterIdAndLevel(selectMaterList.get(i).getMaterId(),
					selectMaterList.get(i).getMaterLevel()).getMaterValueUndressed();
			BigDecimal fraction = selectMaterList.get(i).getMaterNum();
			allValue = allValue.add(value.multiply(fraction).multiply(new BigDecimal("1000")));
		}
		// ---------------计算菜的总价钱--------------∧
		orderCustPo.setOrdValue(allValue);

		// 增加异常处理判断
		if (selectMaterList.get(0).getUnusualDeal() != 0) {
			orderCustPo.setBuySelfFlag(2);// 集采异常
		}
		selectFoodDaoImpl.addSelectFood(orderCustPo);
		return orderCustPo;
	}

	@Override
	public int addMaterOrderForSelfForunusual(List<SelectMaterVo> selectMaterList, UserPo po) throws GenericException {
		OrderCustPo custPo = new OrderCustPo();
		custPo.setOrdCustId((int) po.getUserId());
		// 跳过审核状态
		custPo.setOrdState(4);
		custPo.setUpdateperson((int) po.getUserId());
		custPo.setOrdDate(selectMaterList.get(0).getOrderDate());
		custPo.setOrdDay(selectMaterList.get(0).getOrderDay());
		custPo.setBuySelfFlag(1);// 增加自采标记
		// ---------------计算菜的总价钱--------------∨
		BigDecimal allValue = new BigDecimal(0);
		for (int i = 0; i < selectMaterList.size(); i++) {
			allValue = allValue.add(selectMaterList.get(i).getMaterValueAll());
		}
		// ---------------计算菜的总价钱--------------∧
		custPo.setOrdValue(allValue);
		selectFoodDaoImpl.addSelectFood(custPo);
		return custPo.getOrdId();
	}

	public int addMaterOrderForSelf(List<SelectMaterVo> selectMaterList, long shopId) throws GenericException {
		OrderCustPo custPo = new OrderCustPo();
		custPo.setOrdCustId((int) shopId);
		// 跳过审核状态
		custPo.setOrdState(4);
		custPo.setUpdateperson((int) shopId);
		custPo.setOrdDate(selectMaterList.get(0).getOrderDate());
		custPo.setOrdDay(selectMaterList.get(0).getOrderDay());
		custPo.setBuySelfFlag(1);// 增加自采标记
		custPo.setSubmitOrdTimeLast(PubFun.getCurrentDate() + " " + PubFun.getCurrentTime());
		// ---------------计算菜的总价钱--------------∨
		BigDecimal allValue = new BigDecimal(0);
		for (int i = 0; i < selectMaterList.size(); i++) {
			allValue = allValue.add(selectMaterList.get(i).getMaterValueUndressed().multiply(selectMaterList.get(i).getMaterNum()));
		}
		// ---------------计算菜的总价钱--------------∧
		custPo.setOrdValue(allValue);
		selectFoodDaoImpl.addSelectFood(custPo);
		return custPo.getOrdId();
	}

	@Override
	public void updateMaterOrder(List<SelectMaterVo> selectMaterList, int ordId) throws GenericException {
		OrderCustPo orderCustPo = new OrderCustPo();
		orderCustPo = selectFoodDaoImpl.getOrderCust(String.valueOf(ordId));
		orderCustPo.setSubmitOrdTimeLast(PubFun.getCurrentDate() + " " + PubFun.getCurrentTime());

		// ---------------计算菜的总价钱--------------∨
		BigDecimal allValue = new BigDecimal(0);
		for (int i = 0; i < selectMaterList.size(); i++) {
			BigDecimal value = selectFoodDaoImpl.getMaterLevelByMaterIdAndLevel(selectMaterList.get(i).getMaterId(),
					selectMaterList.get(i).getMaterLevel()).getMaterValueUndressed();
			BigDecimal fraction = selectMaterList.get(i).getMaterNum();
			allValue = allValue.add(value.multiply(fraction).multiply(new BigDecimal("1000")));
		}
		// ---------------计算菜的总价钱--------------∧
		orderCustPo.setOrdValue(allValue.add(orderCustPo.getOrdValue()));
		orderCustPo.setOrdState(4);
		selectFoodDaoImpl.updateSelectFood(orderCustPo);
	}

	public void updateMaterOrderForSelf(int ordId) throws GenericException {
		OrderCustPo orderCustPo = new OrderCustPo();
		orderCustPo = selectFoodDaoImpl.getOrderCust(String.valueOf(ordId));
		orderCustPo.setSubmitOrdTimeLast(PubFun.getCurrentDate() + " " + PubFun.getCurrentTime());

		List<OrderCustFoodPo> orderCustFoodPoList = selectFoodDaoImpl.getOrderCustFoodByOrdId(ordId);
		// ---------------计算菜的总价钱--------------∨
		BigDecimal allValue = new BigDecimal(0);
		for (int i = 0; i < orderCustFoodPoList.size(); i++) {
			allValue = allValue.add(orderCustFoodPoList.get(i).getOrdMaterValue());
		}
		// ---------------计算菜的总价钱--------------∧
		orderCustPo.setOrdValue(allValue);
		selectFoodDaoImpl.updateSelectFood(orderCustPo);
	}

	public boolean checkMaterIsInUseSup(List<SelectFoodVo> selectFoodList, UserPo po) throws GenericException {

		int userId = (int) po.getUserId();
		for (int i = 0; i < selectFoodList.size(); i++) {
			// 首先得到 食品所需材料list
			List<FoodMaterPo> tFoodMaterPoList = selectFoodDaoImpl.getFootMaterByFoodId(selectFoodList.get(i).getFoodId());
			if (tFoodMaterPoList != null && tFoodMaterPoList.size() > 0) {
				for (FoodMaterPo foodMaterPo : tFoodMaterPoList) {
					List<MaterLevelPo> tMaterLevelPoList = selectFoodDaoImpl.checkMaterLevelByUserSup(foodMaterPo.getMaterId(), userId);
					if (tMaterLevelPoList == null || tMaterLevelPoList.size() == 0) {
						msg = "msg001|" + foodMaterPo.getMaterId();
						return false;
					}
				}
			}

		}
		return true;
	}

	public int checkCommitMaterOrder(String orderDate, long shopId, int buySelfFlag) throws GenericException {

		List<OrderCustPo> list = selectFoodDaoImpl.getOrderCustListByOrderDateAndShopId(orderDate, shopId, buySelfFlag);
		if (list != null && list.size() > 0) {
			msg = "msg002";
			return list.get(0).getOrdId();
		}
		return 0;
	}

	// 供货商新增订单校验
	public int checkCommitMaterOrderForSup(String orderDate, long shopId, long supId) throws GenericException {

		List<OrderSupplierPo> list = selectFoodDaoImpl.getOrderCustListByOrderDateAndShopIdAndSupId(orderDate, shopId, supId);
		if (list != null && list.size() > 0) {
			msg = "msg002";
			return list.get(0).getOrdId();
		}
		return 0;
	}

	@Override
	public void deleteOrder(int ordId) throws GenericException {
		selectFoodDaoImpl.deleteOrder(ordId);
	}

	@Override
	public void deleteSupOrderByOid(int ordid) throws GenericException {
		selectFoodDaoImpl.deleteSupOrderByOid(ordid);
	}

	/**
	 * 如果提交过则返回true
	 * */
	@Override
	public boolean checkCommitDeliverState(int ordId, int ordSupId) throws GenericException {
		OrderSupplierPo orderSupplierPo = selectFoodDaoImpl.getOrderSupplierPoByOrdId(ordId, ordSupId);

		if (orderSupplierPo.getCommitDeliverState() != 0) {
			msg = "msg003";
			return true;
		} else if (orderSupplierPo.getOrdState() != 4) {
			msg = "msg004";
			return true;
		} else {
			return false;
		}
	}

}
